<?php

namespace YmPhp\Admin\Http\Controllers\Channel\Wechat;

use YmPhp\Admin\Models\Member\MemberList;
use YmPhp\Admin\Http\Repositories\Channel\Wechat\WechatMember;
use YmPhp\Admin\Form;
use YmPhp\Admin\Grid;
use YmPhp\Admin\Show;
use YmPhp\Admin\Http\Controllers\AdminController;

class WechatMemberController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WechatMember(), function (Grid $grid) {
            $grid->model()->orderBy('uid', 'desc');
            $grid->column('uid')->sortable();
            $grid->column('openid',__('wechat.openid'));
            $grid->column('member',__('member.username'))->display(function () {
                $member = (new MemberList())->getDetails($this->uid);
                return $member->username ?? '';
            });
            $grid->column('unionid',__('wechat.unionid'));
            $grid->column('mobile',__('wechat.mobile'));
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->disableEditButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('uid');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WechatMember(), function (Show $show) {
            $show->field('uid');
            $show->field('openid');
            $show->field('status');
            $show->field('isregister');
            $show->field('unionid');
            $show->field('mobile');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WechatMember(), function (Form $form) {
            $form->display('uid');
            $form->text('openid');
            $form->text('status');
            $form->text('isregister');
            $form->text('unionid');
            $form->text('mobile');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
